package util.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.wq.cycling.R;


/**
 * 通用dialog
 * author：wangqi on 2017/4/26 17:48
 * email：773630555@qq.com
 */

public class DialogFromMiddle {

	private Context context;
	private Dialog dialog;
	private TextView title, content1, content2, content3, sureButton, sureButton2, cancelButton;
	private View sureAndCancel, onlySure;

	public DialogFromMiddle(Context context) {
		super();
		this.context = context;
		// TODO Auto-generated constructor stub
	}

	/**
	 * 显示简单的dialog
	 * @param titleName
	 *            标题
	 * @param contentLine1
	 *            第一行正文
	 * @param contentLine2
	 *            第二行正文传空 表示不显示
	 * @param contentLine3
	 *            第三行正文传空号 表示不显示
	 * @param showWhichSure
	 *            显示哪个确定布局 返回 true 显示带确认 取消的布局 返回 false 显示只带确认的布局
	 * @param sureText
	 *            控制确定按钮是否改变文字 返回空 不改变
	 * @param cancleText
	 *            控制取消按钮是否改变文字 返回空 不改变
	 */
	public void show(String titleName, String contentLine1, String contentLine2, String contentLine3, String sureText,
			String cancleText, boolean showWhichSure) {
		View view = LayoutInflater.from(context).inflate(R.layout.dialog_from_middle, null);

		dialog = new AlertDialog.Builder(context).create();
		dialog.setCancelable(false);
		dialog.show();
		dialog.setCanceledOnTouchOutside(false);
		dialog.getWindow().setContentView(view);
		
		title = (TextView) view.findViewById(R.id.tv_dialog_title);
		content1 = (TextView) view.findViewById(R.id.tv_dialog_content1);
		content2 = (TextView) view.findViewById(R.id.tv_dialog_content2);
		content3 = (TextView) view.findViewById(R.id.tv_dialog_content3);
		sureAndCancel = view.findViewById(R.id.ll_dialog_sure_and_cancel);
		onlySure = view.findViewById(R.id.ll_dialog_only_sure);
		sureButton = (TextView) view.findViewById(R.id.tv_sure);
		sureButton2 = (TextView) view.findViewById(R.id.tv_only_sure);
		cancelButton = (TextView) view.findViewById(R.id.tv_cancel);

		title.setText(titleName);
		content1.setText(contentLine1);
		//根据String判断是否显示第二跟第三行
		if (null != contentLine2 && contentLine2.equals("")) {
			content2.setVisibility(View.GONE);
		} else {
			content2.setVisibility(View.VISIBLE);
			content2.setText(contentLine2);
		}
		if (null != contentLine3 && contentLine3.equals("")) {
			content3.setVisibility(View.GONE);
		} else {
			content3.setVisibility(View.VISIBLE);
			content3.setText(contentLine3);
		}
		//根据boolean判断显示哪种确定布局
		if (showWhichSure) {
			sureAndCancel.setVisibility(View.VISIBLE);
			onlySure.setVisibility(View.GONE);
			//根据String判断是否更改文字
			if (null != sureText && !sureText.equals("")) {
				sureButton.setText(sureText);
			}
			if (null != cancleText && !cancleText.equals("")) {
				cancelButton.setText(cancleText);
			}
		} else {
			sureAndCancel.setVisibility(View.GONE);
			onlySure.setVisibility(View.VISIBLE);
			//根据String判断是否更改文字
			if (null != sureText && !sureText.equals("")) {
				sureButton2.setText(sureText);
			}
		}

		sureButton.setOnClickListener(listener);
		sureButton2.setOnClickListener(listener);
		cancelButton.setOnClickListener(listener);
	}

	private OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.tv_sure:
				sure.onSure();
				dialog.dismiss();
				break;
			case R.id.tv_only_sure:
				sure.onSure();
				dialog.dismiss();
				break;
			case R.id.tv_cancel:
				if (mCancel != null) {
					mCancel.onCancel();
				}
				dialog.dismiss();
				break;

			default:
				break;
			}
		}
	};

	public void dismiss() {
		dialog.dismiss();
	}

	public static interface Sure {
		void onSure();
	}

	public Sure sure;

	/**
	 * 点击确定回调的监听
	 * 
	 * @param sure
	 */
	public void setSureListener(Sure sure) {
		this.sure = sure;
	}

	public static interface MCancel {
		void onCancel();
	}

	public MCancel mCancel;

	/**
	 * 点击取消回调的监听
	 * 
	 * @param sure
	 */
	public void setCancelListener(MCancel mCancel) {
		this.mCancel = mCancel;
	}
}
